﻿using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using WalkingTec.Mvvm.Core;

namespace WTM
{
    public class Program
    {
       

        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateWebHostBuilder(string[] args)
        {

             var builder_config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory()) // 设置配置文件的基路径为当前目录
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); // 添加 appsettings.json 文件                                                            // 构建 IConfiguration 实例
            var configuration = builder_config.Build();
            // 通过键名获取配置值
            var Server_Port = configuration["Server_Port"];

            return
                Host.CreateDefaultBuilder(args)
                  .ConfigureAppConfiguration((hostingContext, config) =>
                  {
                      config.AddInMemoryCollection(new Dictionary<string, string> { { "HostRoot", hostingContext.HostingEnvironment.ContentRootPath } });
                  })
                 .ConfigureLogging((hostingContext, logging) =>
                 {
                     logging.ClearProviders();
                     logging.AddConsole();
                     logging.AddWTMLogger();
                 })
                .ConfigureWebHostDefaults(webBuilder =>
                 {
                     webBuilder.UseKestrel(options =>
                     {
                         options.ListenAnyIP(int.Parse(Server_Port));
                     });
                     webBuilder.UseStartup<Startup>();
                 });
        }
    }
}
